package com.qiaomengapp.serviceImp;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qiaomengapp.bean.Color;
import com.qiaomengapp.bean.GoodTypelevel1;
import com.qiaomengapp.bean.GoodTypelevel2;
import com.qiaomengapp.bean.ProviderGood;
import com.qiaomengapp.bean.ShopGood;
import com.qiaomengapp.bean.ShopGoodWrap;
import com.qiaomengapp.bean.ShopWarp;
import com.qiaomengapp.bean.Size;
import com.qiaomengapp.dao.ShopGoodMapper;
import com.qiaomengapp.dao.ShopMapper;
import com.qiaomengapp.exception.CustomException;
import com.qiaomengapp.service.IShopGoodService;

@Service
public class ImpShopGoodService implements IShopGoodService{
	@Autowired
	private ShopGoodMapper shopGoodMapper;
	@Autowired
	private ShopMapper shopMapper;

	// 通过shop_good_id来修改商品的上架状态1-上架
	public void update_statusTo1(int shit_id) throws CustomException {
		int i = shopGoodMapper.update_statusTo1(shit_id);
		if (i == 1) {
			return;
		} else {
			throw new CustomException("上架失败");
		}
	}

	// 更新上架时间
	public void update_launch_time(int id) throws CustomException {
		int i = shopGoodMapper.update_launch_time(id);
		if (i == 1) {
			return;
		} else {
			throw new CustomException("更新上架时间失败");
		}
	}

	// 下架
	public void update_statusTo2(int shit_id) throws CustomException {
		int i = shopGoodMapper.update_statusTo2(shit_id);
		if (i == 1) {
			return;
		} else {
			throw new CustomException("下架失败");
		}
	}

	// 显示销售中的商品
	public List<ShopGood> select_ShopGood(int shop_id) throws CustomException {
		List<ShopGood> list = shopGoodMapper.select_ShopGood(shop_id);
		if (list.size() == 0 || list == null) {
			throw new CustomException("显示销售中的商品失败");
		} else {
			return list;
		}
	}
	// 显示wei销售中的商品
   public List<ShopGood> select_ShopGoodStatus2(int shop_id) throws CustomException{
		List<ShopGood> list = shopGoodMapper.select_ShopGoodStatus2(shop_id);
		if (list.size() == 0 || list == null) {
			throw new CustomException("显示wei销售中的商品失败");
		} else {
			return list;
		}
	}

	// 显示销售的商品并按销量排序
	public List<ShopGood> select_ShopGoodBySaleCount(int shop_id) throws CustomException {
		List<ShopGood> list = shopGoodMapper.select_ShopGoodBySaleCount(shop_id);
		if (list.size() == 0 || list == null) {
			throw new CustomException("显示销售的商品并按销量排序失败");
		} else {
			return list;
		}
	}

	// 显示销售的商品并按佣金排序
	public List<ShopGood> select_ShopGoodByCommission(int shop_id) throws CustomException {
		List<ShopGood> list = shopGoodMapper.select_ShopGoodByCommission(shop_id);
		if (list.size() == 0 || list == null) {
			throw new CustomException("显示销售的商品并按佣金排序失败");
		} else {
			return list;
		}
	}

	// 显示销售的商品并按售价排序
	public List<ShopGood> select_ShopGoodByPrice(int shop_id) throws CustomException {
		List<ShopGood> list = shopGoodMapper.select_ShopGoodByPrice(shop_id);
		if (list.size() == 0 || list == null) {
			throw new CustomException("显示销售的商品并按售价排序失败");
		} else {
			return list;
		}
	}

	// 显示销售的商品并按收藏排序
	public List<ShopGood> select_ShopGoodByfocus_num(int shop_id) throws CustomException {
		List<ShopGood> list = shopGoodMapper.select_ShopGoodByfocus_num(shop_id);
		if (list.size() == 0 || list == null) {
			throw new CustomException("显示销售的商品并按收藏排序失败");
		} else {
			return list;
		}
	}

	// 显示一级分类
	public List<GoodTypelevel1> select_GoodTypelevel1() throws CustomException {
		List<GoodTypelevel1> list = shopGoodMapper.select_GoodTypelevel1();
		if (list.size() == 0 || list == null) {
			throw new CustomException("显示一级分类失败");
		} else {
			return list;
		}
	}

	// 点击一级分类显示二级分类
	public List<GoodTypelevel2> select_GoodTypelevel2(int id)
			throws CustomException {
		List<GoodTypelevel2> list = shopGoodMapper.select_GoodTypelevel2(id);
		if (list.size() == 0) {
			throw new CustomException("显示二级分类失败");
		} else {
			return list;
		}
	}

	// 点击二级分类获取所有二级分类的商品
	public List<ShopGood> select_G2_good(int id) throws CustomException {
		List<ShopGood> list = shopGoodMapper.select_G2_good(id);
		if (list.size() == 0 || list == null) {
			throw new CustomException("显示二级分类失败");
		} else {
			return list;
		}
	}

	// 删除商品
	public void delete_shopGood(int shopGoodId) throws CustomException {
		int i = shopGoodMapper.delete_shopGood(shopGoodId);
		if (i == 1) {
			return;
		} else {
			throw new CustomException("删除商品失败");
		}
	}

	// 查看商品的详情
	public ShopGoodWrap select_ShopGoodWrap(int id) throws CustomException {
		ShopGoodWrap wrap = shopGoodMapper.select_ShopGoodWrap(id);
		if (wrap == null) {
			throw new CustomException("查看商品的详情失败");
		} else {
			return wrap;
		}
	}

	// 添加商品,前台页面显示店铺可添加的provide_good的信息，在shopgood表中添加相应的数据
	public void insert_Good(ProviderGood pro, int userId)
			throws CustomException {
		//根据userId查询商铺
		ShopWarp shopWarp = shopMapper.select_shopByUid(userId);
		if (shopWarp==null) {
			throw new CustomException("该用户不具有商铺！");
		}
		int i = shopGoodMapper.insert_Good(pro, shopWarp.getId());
		if (i != 1){
			throw new CustomException("添加商品失败");
		}
	}

	// 收藏商品,从session取出登陆用户的id，获取收藏商品的id，shopid
	// 1.在user_focus表中添加一条记录
	public void insert_user_focus(int user_id, int shop_good_id)
			throws CustomException {
		int i = shopGoodMapper.insert_user_focus(user_id, shop_good_id);
		if (i == 1) {
			return;
		} else {
			throw new CustomException("在user_focus表中添加一条记录失败");
		}
	}

	// 2.把shopgood中的相应id的focus_num的值加 一
	public void update_shopGoodFocusNum(int id) throws CustomException {
		int i = shopGoodMapper.update_addshopGoodFocusNum(id);
		if (i == 1) {
			return;
		} else {
			throw new CustomException("把shopgood中的相应id的focus_num的值加   一失败");
		}
	}

	// 收藏店铺
	public void insert_shop(int user_id, int shop_id)
			throws CustomException {
		int i = shopGoodMapper.insert_shop(user_id, shop_id);
		if (i == 1) {
			return;
		} else {
			throw new CustomException("insert_shop失败");
		}
	}

	public void update_shop_addcollectNum(int id) throws CustomException {
		int i = shopGoodMapper.update_shop_addcollectNum(id);
		if (i == 1) {
			return;
		} else {
			throw new CustomException("insert_shop失败");
		}
	}

	// 显示商品的参数,color,size
	public List<Color> select_color() throws CustomException {
		List<Color> list = shopGoodMapper.select_color();
		if (list.size() == 0 || list == null) {
			throw new CustomException("显示商品color失败");
		} else {
			return list;
		}
	}

	public List<Size> select_size() throws CustomException {
		List<Size> list = shopGoodMapper.select_size();
		if (list.size() == 0 || list == null) {
			throw new CustomException("显示商品size失败");
		} else {
			return list;
		}
	}

	// 店铺预览，显示所有的商品数量
	public int select_shopGoodNum(int shop_id) throws CustomException {
		List<ShopGood> list = shopGoodMapper.select_ShopGood(1);
		if (list.size() == 0 || list == null) {
			throw new CustomException("显示商品失败");
		} else {
			int i = shopGoodMapper.select_shopGoodNum(shop_id);
			return i;
		}
	}

	// 选择出所有上架商品的上架日期
	public List<Date> select_launchTime() throws CustomException {
		List<Date> list = shopGoodMapper.select_launchTime();
		if (list.size() == 0 || list == null) {
			throw new CustomException("选择出所有上架商品的上架日期失败");
		} else {
			return list;
		}
	}

	// //使用datediff来计算上架日期在一个月内的商品，需要字符串日期，使用Mysql中的datefiff函数来计算和当前时间的天数差
	public int select_shopGoodByTime(String time) {
		int i = shopGoodMapper.select_shopGoodByTime(time);
		return i;
	}

}
